using System;
using System.Collections.Generic;
using FrameIdentityReq.Network;

namespace FrameIdentityReq.Frames
{

    /// <summary>
    /// 身份验证回应帧
    /// </summary>
    public class IdentityAckFrame : BaseFrame
    {
        public static new event EventHandler<SocketState> OnRestored;

        /// <summary>
        /// 硬件号. 16 bytes. 紧邻 InfoLength 之后.
        /// </summary>
        public IStringField HardwareSerialNum;


        /// <summary>
        /// 返回验证结果 ，1字节
        /// </summary>
        public IValidationCodeField ValidationCode;

        /// <summary>
        /// 通讯号. 3 Bytes. 由服务器分配
        /// </summary>
        public IBytesField DeviceCommunicateID;

        /// <summary>
        /// 时间. 4 Bytes
        /// </summary>
        public IDateTimeField ServerDateTime;

        public IdentityAckFrame() :
        base()
        {
            FrameType.Value = FrameTypeEnum.IDENTITY_ACK;
            InfoLength.Value = BitsField.CeilingByteCount(ValidationCode.Count + DeviceCommunicateID.Count + ServerDateTime.Count); /* 理论值应该 = 8 */
        }

        public IdentityAckFrame(IList<byte> buffer) :
        base(buffer)
        { }

        protected override IField AddBodyFields(IField field)
        {
            HardwareSerialNum = new StringField(field, 16 << 3, "HardwareSerialNum");
            ValidationCode = new ValidationCodeField(HardwareSerialNum, "HardwareSerialNum");
            DeviceCommunicateID = new BitsField(ValidationCode, 3 << 3, "DeviceCommunicateID");
            ServerDateTime = new DateTimeField(DeviceCommunicateID, "ServerDateTime");

            return ServerDateTime;
        }

        protected override void RaiseRestored(SocketState state)
        {
            if (OnRestored != null)
                OnRestored(this, state);
        }
    }
}